package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._

object Demo4OnKafka {
  def main(args: Array[String]): Unit = {

    //创建flink的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //流模式
      .build()

    /**
      * 创建flink sql 环境
      *
      */

    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * 创建表
      *
      */

    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student (
        | id STRING,
        | name STRING,
        | age BIGINT,
        | gender STRING,
        | clazz STRING
        |) WITH (
        | 'connector' = 'kafka',
        | 'topic' = 'student',
        | 'properties.bootstrap.servers' = 'master:9092',
        | 'properties.group.id' = 'qweqweqwe',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'format' = 'csv',
        | 'csv.field-delimiter' = ',',
        | 'csv.ignore-parse-errors' = 'true'
        |
        |)
        |
      """.stripMargin)


    /**
      * 创建print 表打印结果
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table
        |(
        |clazz STRING,
        |num BIGINT
        |)
        |WITH ('connector' = 'print')
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |
        |insert into print_table
        |select clazz,count(1) as num from student group by clazz
        |
      """.stripMargin)



  }

}
